<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      v-dialogDrag
      append-to-body
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        v-loading="formLoading"
        label-width="100px"
      >
        <el-form-item label="轮播图ID" prop="bannerId">
          <el-input v-model="formData.bannerId" placeholder="请输入轮播图ID" />
        </el-form-item>
        <el-form-item label="导航栏ID" prop="navId">
          <el-input v-model="formData.navId" placeholder="请输入导航栏ID" />
        </el-form-item>
        <el-form-item label="首页平铺ID" prop="tileId">
          <el-input v-model="formData.tileId" placeholder="请输入首页平铺ID" />
        </el-form-item>
        <el-form-item label="方案ID" prop="planId">
          <el-input v-model="formData.planId" placeholder="请输入方案ID" />
        </el-form-item>
        <el-form-item label="广告图片ID" prop="adpictureId">
          <el-input
            v-model="formData.adpictureId"
            placeholder="请输入广告图片ID"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as HomepageApi from "@/api/system/homepage";
export default {
  name: "HomepageForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        bannerId: undefined,
        navId: undefined,
        tileId: undefined,
        planId: undefined,
        adpictureId: undefined,
      },
      // 表单校验
      formRules: {
        bannerId: [
          { required: true, message: "轮播图ID不能为空", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "轮播图ID必须为数字", trigger: "blur" },
        ],
        navId: [
          { required: true, message: "导航栏ID不能为空", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "导航栏ID必须为数字", trigger: "blur" },
        ],
        tileId: [
          { required: true, message: "首页平铺ID不能为空", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "首页平铺ID必须为数字", trigger: "blur" },
        ],
        planId: [
          { required: true, message: "方案ID不能为空", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "方案ID必须为数字", trigger: "blur" },
        ],
        adpictureId: [
          { required: true, message: "广告图片ID不能为空", trigger: "blur" },
          { pattern: /^[0-9]*$/, message: "广告图片ID必须为数字", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await HomepageApi.getHomepage(id);
          this.formData = res.data;
          this.dialogTitle = "修改首页信息";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增首页信息";
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await HomepageApi.updateHomepage(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await HomepageApi.createHomepage(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        bannerId: undefined,
        navId: undefined,
        tileId: undefined,
        planId: undefined,
        adpictureId: undefined,
      };
      this.resetForm("formRef");
    },
  },
};
</script>